package org.convallaria.system.biz.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.convallaria.framework.common.result.ApiResult;
import org.convallaria.framework.security.annotation.Anonymous;
import org.convallaria.infrastruct.db.entity.PageResult;
import org.convallaria.system.biz.entity.dto.sys.*;
import org.convallaria.system.biz.entity.vo.sys.UserVO;
import org.convallaria.system.biz.service.sys.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 系统用户控制器
 * 
 * @author gjh
 * @since 2024-09-22
 */
@Tag(name = "用户管理", description = "系统用户管理相关接口")
@Slf4j
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询用户列表
     */
    @Operation(summary = "分页查询用户列表", description = "根据条件分页查询系统用户列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功", 
                    content = @Content(schema = @Schema(implementation = IPage.class))),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PostMapping("/page")
    public ApiResult<PageResult<UserVO>> pageUsers(@Validated @RequestBody UserQuery query) {
        return ApiResult.success(PageResult.of(sysUserService.pageUsers(query)));
    }

    /**
     * 获取用户详情
     */
    @Operation(summary = "获取用户详情", description = "根据用户ID获取用户详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "用户不存在"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @GetMapping("/{userId}")
    public ApiResult<UserVO> getDetail(@PathVariable Long userId) {
        return ApiResult.success(sysUserService.getDetail(userId));
    }

    /**
     * 新增用户
     */
    @Operation(summary = "新增用户", description = "创建新的系统用户")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PostMapping
    public ApiResult<Void> addUser(@Validated @RequestBody UserCreateDTO userDTO) {
        sysUserService.addUser(userDTO);
        return ApiResult.success();
    }

    /**
     * 修改用户
     */
    @Operation(summary = "修改用户", description = "更新用户信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "404", description = "用户不存在"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PutMapping
    public ApiResult<Void> editUser(@Validated @RequestBody UserUpdateDTO userDTO) {
        sysUserService.editUser(userDTO);
        return ApiResult.success();
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "根据用户ID列表批量删除用户")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @DeleteMapping
    public ApiResult<Void> deleteUsers(@RequestBody Long[] userIds) {
        sysUserService.deleteUsers(List.of(userIds));
        return ApiResult.success();
    }

    /**
     * 重置密码
     */
    @Operation(summary = "重置密码", description = "重置用户密码")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "重置成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "404", description = "用户不存在"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PutMapping("/resetPwd")
    public ApiResult<Void> resetPwd(@RequestBody ResetPwdDTO resetPwdDTO) {
        sysUserService.resetPwd(resetPwdDTO);
        return ApiResult.success();
    }

    /**
     * 修改密码
     */
    @Operation(summary = "修改密码", description = "用户修改自己的密码")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PutMapping("/changePwd")
    public ApiResult<Void> changePwd(@Validated @RequestBody ChangePwdDTO changePwdDTO) {
        sysUserService.changePwd(changePwdDTO);
        return ApiResult.success();
    }

    /**
     * 修改个人资料
     */
    @Operation(summary = "修改个人资料", description = "用户修改自己的个人资料")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PutMapping("/profile")
    public ApiResult<Void> updateProfile(@Validated @RequestBody UserProfileDTO userProfileDTO) {
        sysUserService.updateProfile(userProfileDTO);
        return ApiResult.success();
    }

    /**
     * 上传头像
     */
    @Operation(summary = "上传头像", description = "用户上传头像")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "上传成功"),
            @ApiResponse(responseCode = "400", description = "文件格式错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PostMapping("/avatar")
    public ApiResult<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return ApiResult.success(sysUserService.uploadAvatar(file));
    }

    /**
     * 导出用户
     */
    @Operation(summary = "导出用户", description = "导出用户列表到Excel")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "导出成功"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PostMapping("/export")
    public void exportUsers(@RequestBody UserQuery query, HttpServletResponse response) {
        sysUserService.exportUsers(query, response);
    }

    /**
     * 下载导入模板
     */
    @Operation(summary = "下载导入模板", description = "下载用户导入Excel模板")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "下载成功"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @GetMapping("/importTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        sysUserService.downloadTemplate(response);
    }

    /**
     * 导入用户
     */
    @Operation(summary = "导入用户", description = "从Excel文件导入用户")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "导入成功"),
            @ApiResponse(responseCode = "400", description = "文件格式错误"),
            @ApiResponse(responseCode = "500", description = "系统异常")
    })
    @PostMapping("/import")
    public ApiResult<Void> importUsers(@RequestParam("file") MultipartFile file) {
        sysUserService.importUsers(file);
        return ApiResult.success();
    }
}
